{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Save Intermediate Feature Matrix Results\n",
    "\n",
    "In this tutorial, we will go over the how to save intermediate results when computing the feature matrix."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import featuretools as ft"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this example, we will use a dataset of retail data of customers from a UK website from December 2010 to December 2011."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "es = ft.demo.load_retail(nrows=10000)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "let's use a simple feature for this example."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "region = ft.Feature(es[\"customers\"][\"Country\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can supply “cutoff times” to specify that we want to calculate features one year after a customer’s first invoice."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "cutoff_times = es[\"customers\"].df[[\"CustomerID\", \"first_invoices_time\"]].rename(\n",
    "    columns={\"CustomerID\": \"instance_id\", \"first_invoices_time\": \"time\"})\n",
    "cutoff_times[\"time\"] = cutoff_times[\"time\"] + pd.Timedelta(\"365 days\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<p>Here is what some of the cutoff times look like.</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>instance_id</th>\n",
       "      <th>time</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CustomerID</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>17850.0</th>\n",
       "      <td>17850.0</td>\n",
       "      <td>2011-12-01 08:26:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13047.0</th>\n",
       "      <td>13047.0</td>\n",
       "      <td>2011-12-01 08:34:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12583.0</th>\n",
       "      <td>12583.0</td>\n",
       "      <td>2011-12-01 08:45:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13748.0</th>\n",
       "      <td>13748.0</td>\n",
       "      <td>2011-12-01 09:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15100.0</th>\n",
       "      <td>15100.0</td>\n",
       "      <td>2011-12-01 09:09:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15291.0</th>\n",
       "      <td>15291.0</td>\n",
       "      <td>2011-12-01 09:32:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14688.0</th>\n",
       "      <td>14688.0</td>\n",
       "      <td>2011-12-01 09:37:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14527.0</th>\n",
       "      <td>14527.0</td>\n",
       "      <td>2011-12-01 09:41:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15311.0</th>\n",
       "      <td>15311.0</td>\n",
       "      <td>2011-12-01 09:41:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17809.0</th>\n",
       "      <td>17809.0</td>\n",
       "      <td>2011-12-01 09:41:00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            instance_id                time\n",
       "CustomerID                                 \n",
       "17850.0         17850.0 2011-12-01 08:26:00\n",
       "13047.0         13047.0 2011-12-01 08:34:00\n",
       "12583.0         12583.0 2011-12-01 08:45:00\n",
       "13748.0         13748.0 2011-12-01 09:00:00\n",
       "15100.0         15100.0 2011-12-01 09:09:00\n",
       "15291.0         15291.0 2011-12-01 09:32:00\n",
       "14688.0         14688.0 2011-12-01 09:37:00\n",
       "14527.0         14527.0 2011-12-01 09:41:00\n",
       "15311.0         15311.0 2011-12-01 09:41:00\n",
       "17809.0         17809.0 2011-12-01 09:41:00"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cutoff_times.head(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "<p> If you want to save intermediate computations as CSVs, simply pass the location of a directory of where the computation should be saved.\n",
    "For example, if you pass a directory called \"ft_temp\", CSV files will be output to the directory, named according t the timestamp that it represents. </p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "save_progress = os.path.join(os.getcwd(), 'ft_temp')\n",
    "if not os.path.exists(save_progress):\n",
    "    os.makedirs(save_progress)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "fm_save = ft.calculate_feature_matrix([region],\n",
    "                                       entityset=es,\n",
    "                                       cutoff_time=cutoff_times.sample(10),\n",
    "                                       save_progress=save_progress)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<p> As seen below, there are now files in the directory, named by timestamp. </p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ft_2011_12_01_03-08-00-000000.csv  ft_2011_12_02_05-03-00-000000.csv\r\n",
      "ft_2011_12_01_09-00-00-000000.csv  ft_2011_12_02_05-19-00-000000.csv\r\n",
      "ft_2011_12_01_12-43-00-000000.csv  ft_2011_12_02_12-07-00-000000.csv\r\n",
      "ft_2011_12_01_12-51-00-000000.csv  ft_2011_12_02_12-18-00-000000.csv\r\n",
      "ft_2011_12_02_03-19-00-000000.csv  ft_2011_12_03_12-57-00-000000.csv\r\n"
     ]
    }
   ],
   "source": [
    "% ls ft_temp/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import shutil\n",
    "shutil.rmtree(save_progress)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
